using System;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.ManagedCode
{
	/// <summary>
	/// Summary description for DataDOMEvent.
	/// </summary>
	public class DataDOMEvent : Microsoft.Office.Interop.InfoPath.SemiTrust.DataDOMEvent 
	{
		public static string Insert = "Insert";
		public static string Update = "Update";
		public static string Delete = "Delete";

		private Microsoft.Office.Interop.InfoPath.SemiTrust.XDocument xDocument;
		private bool returnStatus;
		private Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNode source;
		private Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNode site;
		private string matchExpression = null;
		private string operation = null;
		private object newValue = null;
		private object oldValue = null;
		private string returnMessage = null;

		private Error error = null;
        //CID 0778 //2011.03.28 //Begin
        private FieldError fieldError = null;
        //CID 0778 //2011.03.28 //End

		private Form form = null;

		public DataDOMEvent( Microsoft.Office.Interop.InfoPath.SemiTrust.XDocument xDoc, Form frm )
		{
			this.xDocument = xDoc;
			this.form = frm;
		}

		public Microsoft.Office.Interop.InfoPath.SemiTrust.XDocument XDocument 
		{ 
			get
			{
				return this.xDocument;
			}
		}

		public bool ReturnStatus 
		{ 
			get
			{
				return this.returnStatus;
			}

			set
			{
				this.returnStatus = value;
			}

		}

		public Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNode Source 
		{ 
			get
			{
				return this.source;
			}

			set
			{
				this.source = value;
			}
		}

		public Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNode Site 
		{ 
			get
			{
				return this.site;
			}

			set
			{
				this.site = value;
			}
		}

		public string MatchExpression 
		{ 
			get
			{
				return matchExpression;
			}

			set
			{
				this.matchExpression = value;
			}
		}

		public Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNode Parent 
		{ 
			get
			{
				if( this.Site is IDOMNode  )
				{
					IDOMNode node = (IDOMNode)this.Site;
					return XMLNode.createXMLNode( this.form, node.XmlNode.ParentNode, this.form.XmlNsResolver );
				}
				return null;
			}
		}

		public string Operation 
		{ 
			get
			{
				return this.operation;
			}

			set
			{
				this.operation = value;
			}
		}

		public object NewValue 
		{ 
			get
			{
				return this.newValue;
			}

			set
			{
				this.newValue = value;
			}
		}

		public object OldValue 
		{ 
			get
			{
				return this.oldValue;
			}
			//CID 0213 //2009.01.22 //Begin
			set
			{
				this.oldValue = value;
			}
			//CID 0213 //2009.01.22 //End
		}

		public string ReturnMessage 
		{ 
			get
			{
				return this.returnMessage;
			}
			set
			{
				this.returnMessage = value;
			}
		}

		public Microsoft.Office.Interop.InfoPath.SemiTrust.ErrorObject ReportError(object varNode, string bstrShortErrorMessage, bool fSiteIndependent )
		{
			IDOMNode domNode = (IDOMNode)varNode;
			Error error = new Error();
			error.Node = domNode;
			error.ConditionName = "";
			error.ShortErrorMessage = bstrShortErrorMessage;
			error.DetailedErrorMessage = "";
			error.ErrorCode = 0;
			error.ErrorType = Error.modeless;

            //CID 0778 //2011.03.28 //Begin
            if (this.fieldError != null)
            {
                this.form.FieldErrorList.Remove(this.fieldError);
            }
            this.fieldError = new FieldError(FieldError.ProgramError, domNode.XmlNode, DOMUtil.getNodeJetId(domNode.XmlNode), bstrShortErrorMessage);
            this.fieldError.ProgramErrorObject = error;
            this.form.FieldErrorList.Add(this.fieldError);
            //CID 0778 //2011.03.28 //End

			this.error = error;
			return this.error;
		}

		public Microsoft.Office.Interop.InfoPath.SemiTrust.ErrorObject ReportError(object varNode, string bstrShortErrorMessage, bool fSiteIndependent, string bstrDetailedErrorMessage, int lErrorCode, string bstrType)
		{
			IDOMNode domNode = (IDOMNode)varNode;
			Error error = new Error();
			error.Node = domNode;
			error.ConditionName = "";
			error.ShortErrorMessage = bstrShortErrorMessage;
			error.DetailedErrorMessage = bstrDetailedErrorMessage;
			error.ErrorCode = lErrorCode;
			error.ErrorType = bstrType;
			if( error.ErrorType == null )
			{
				error.ErrorType = Error.modeless;
			}

            //CID 0778 //2011.03.28 //Begin
            if (this.fieldError != null)
            {
                this.form.FieldErrorList.Remove(this.fieldError);
            }
            this.fieldError = new FieldError(FieldError.ProgramError, domNode.XmlNode, DOMUtil.getNodeJetId(domNode.XmlNode), bstrShortErrorMessage + "\n\n" + bstrDetailedErrorMessage);
            this.fieldError.ProgramErrorObject = error;
            this.form.FieldErrorList.Add(this.fieldError);
            //CID 0778 //2011.03.28 //End

			this.error = error;
			return this.error;
		}

		//Unsupported
		public bool IsUndoRedo 
		{ 
			get
			{
				return false;
			}
		}

		//Used by InfoJet Service
		public Error Error
		{
			get
			{
				return this.error;
			}
		}

	}
}
